package de.justphil.tcg.tcgserver.commons.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.justphil.tcg.tcgserver.commons.domain.wrappers.Credentials;

import net.jcip.annotations.ThreadSafe;

@ThreadSafe
public class StringUtil {
	
	public static final Pattern I18N_WILDCARD_PATTERN 	= Pattern.compile(Pattern.quote("{}"));
	public static final String AUTH_HEADER_SEPERATOR 	= Pattern.quote( ":" );
	
	
	private static final String ALPHABET_LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
	private static final String ALPHABET_UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String DIGITS				= "0123456789";
	private static final String SPECIAL_CHARACTERS	= "_-.";
	
	private static final Map<Character, Boolean> whiteListener;
	
	static {
		whiteListener = new HashMap<Character, Boolean>();
		
		// Put lower case alphabet
		for (int i = 0; i < ALPHABET_LOWER_CASE.length(); i++) {
			whiteListener.put(ALPHABET_LOWER_CASE.charAt(i), true);
		}
		
		// Put upper case alphabet
		for (int i = 0; i < ALPHABET_UPPER_CASE.length(); i++) {
			whiteListener.put(ALPHABET_UPPER_CASE.charAt(i), true);
		}
		
		// Put digits
		for (int i = 0; i < DIGITS.length(); i++) {
			whiteListener.put(DIGITS.charAt(i), true);
		}
		
		// Put lower case alphabet
		for (int i = 0; i < SPECIAL_CHARACTERS.length(); i++) {
			whiteListener.put(SPECIAL_CHARACTERS.charAt(i), true);
		}
	}
	
	
	
	
	public static boolean isNullOrEmpty(String s) {
		return (s == null || s.trim().length() == 0);
	}
	
	public static String replaceWildcards(String s, Object... replacements) {
		return replaceWildcards(I18N_WILDCARD_PATTERN, s, replacements);
	}
	
	public static String replaceWildcards(String wildcardString, String s, Object[] replacements) {
		Pattern p = Pattern.compile(Pattern.quote(wildcardString));
		return replaceWildcards(p, s, replacements);
		
		/*
		int j = 0;
		while ( s.indexOf(wildcardString) != -1 ) {
			if (j < replacements.length)
				s = p.matcher(s).replaceFirst(string(replacements[j]));
			
			j++;
		}
		*/
		
		//return s;
	}
	
	public static String replaceWildcards(Pattern wildcardPattern, String s, Object... replacements) {
		StringBuffer sb = new StringBuffer();
		
		int i = 0;
		Matcher m = wildcardPattern.matcher(s);
		while ( m.find() && i < replacements.length ) {
			m.appendReplacement(sb, string(replacements[i]));
			i++;
		}
		m.appendTail(sb);
		
		return sb.toString();
	}
	
	public static String string(Object o) {
		if (o instanceof String) {
			return (String) o;
		}
		
		return o.toString();
	}
	
	public static Credentials parseAuthorizationHeader(String authorizationHeader) {
		String[] segs = authorizationHeader.split( AUTH_HEADER_SEPERATOR );
		
		if (segs.length < 2) {
			return null;
		}
		
		Credentials cred = new Credentials(segs[0], segs[1]);
		return cred;
	}
	
	public static boolean isValid(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (!whiteListener.containsKey(s.charAt(i))) {
				return false;
			}
		}
		
		return true;
	}
}
